// demo of the hashmap 

#include "../src/map.h"
#include <stdio.h>

// with GCC, build by:
// $ gcc hello_world_demo.c hashmap.c -lrt -o hw
int main() {
	mem_storage *storage = mem_storage_new(0
#ifdef MEM_STORAGE_SUPPORT_THREAD_SAFE
			,true
#endif
			);
	map_fn fns = {str_hash_fn, str_eq_fn, mem_free, mem_free};
	hashmap* h = hmap_new(&fns, storage,
			HEAP_MEM);

	char* s = (char*) mem_storage_alloc(storage, 8, HEAP_MEM);
	char* k = (char*) mem_storage_alloc(storage, 8, HEAP_MEM);

	sprintf(s, "hello");
	sprintf(k, "world");

	hmap_put(h, s, k); // map "hello" to "world"

	char* x = (char*) hmap_get(h, k);
	printf("%s ", x);

	x = (char*) hmap_remove(h, "hello");
	printf("%s!\n", x);
	x = (char*) hmap_remove(h, "hello");
	assert(x == NULL);
	hmap_clear(h, true);
	s = (char*) mem_storage_alloc(storage, 10, HEAP_MEM);
	k = (char*) mem_storage_alloc(storage, 20, HEAP_MEM);
	sprintf(s, "xin chao");
	sprintf(k, "cac ban");
	hmap_put(h, s, k);
	x = (char*) hmap_remove(h, k);
	assert(x == NULL);
	x = (char*) hmap_remove(h, s);
	printf("%s!\n", x);
	hmap_free(h);
	h = hmap_new(&fns, NULL, HEAP_MEM);
	int m = 10;
	int n = 20;
	hmap_put(h, &m, &n);
	m = 100;
	n = 50;
	hmap_put(h, &m, &n);
	int *value = hmap_get(h, &m);
	printf("%d=%d, ", m, *value);
	m = 10;
	value = hmap_get(h, &m);
	printf("%d=%d", m, *value);
	return 0;
}
